import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import streamlit as st


def main():
    st.set_page_config(layout="wide")
    # 展示文本；文本直接使用Markdown语法
    st.markdown(
        '''
# Faster RCNN(FPN) in tensorflow
## Background
Feature Pyramid Network (FPN) is a architecture that is top-down architecture with lateral connections is developed for building high-level semantic feature maps at all scales. Using FPN in a basic Faster R-CNN system, the method achieves state-of-the-art singlemodel results on the VOC detection benchmark. This folder contains an implementation of Faster RCNN(FPN) for the VOC dataset written in TensorFlow.

See the following papers for more background:

[1] Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks by Shaoqing Ren, Kaiming He, Ross Girshick and Jian Sun, Jan 2016.

[2] Feature Pyramid Networks for Object Dtection by Tsung-Yi Lin, Piotr Dollar, Ross Girshick, Kaiming He, Bharath Hariharan and Serge Belongie, Apr 2017.
## Compile
```
cd $PATH_ROOT/libs/box_utils/cython_utils
python setup.py build_ext --inplace
```
## Model
Please download [resnet50_v1](http://download.tensorflow.org/models/resnet_v1_50_2016_08_28.tar.gz)、[resnet101_v1](http://download.tensorflow.org/models/resnet_v1_101_2016_08_28.tar.gz) pre-trained models on Imagenet, put it to $PATH_ROOT/data/pretrained_weights.

Please download [trained model](https://github.com/DetectionTeamUCAS/Models/tree/master/FPN_Tensorflow) by this project, then put it to $PATH_ROOT/output/trained_weights.
## Dataset
### VOC
To begin, you will need to download the VOC dataset. If you want to train the model, you'll convert it to TFRecord format. The following script provide a few options.
```
cd $PATH_ROOT/data/io/
python convert_data_to_tfrecord.py --VOC_dir='/PATH/TO/VOCdevkit/VOCdevkit_train/'
                                   --xml_dir='Annotation'
                                   --image_dir='JPEGImages'
                                   --save_name='train'
                                   --img_format='.jpg'
                                   --dataset='pascal'
```
In D3 servers(10.175.122.74 etc.), the available dataset is located at:
```bash
/development/l00506990/Dataset
```
## Environment
Python3.5.2, CUDA 9.0, cuDNN 7.4.1, Tensorflow 1.10.
Other related third-party python libs, if any, are listed in the requirement.txt.
## Train and evaluate process
Once your dataset is ready, you can begin training the model as follows:
```bash
sh fasterrcnn_baseline_original.sh
```
The corresponding training hyperparameter is shown as follows:
      Learning rate policy
      Step
      Initial learning rate
      0.001
      Batch size
      1
      Max iteration
      150000
      Weight decay
      0.00001
With pretrained model, you can evaluate the model on the VOC validation set, as follows:
```bash
sh fasterrcnn_baseline_eval.sh
```
The validation results are shown as follows:
| Models          | mAP   | sheep | horse | bicycle | bottle | cow   | sofa  | bus   | dog   | cat   | person | train | diningtable | aeroplane | car   | pottedplant | tvmonitor | chair      | bird  | boat  | motorbike |
| --------------- | ----- | ----- | ----- | ------- | ------ | ----- | ----- | ----- | ----- | ----- | ------ | ----- | ----------- | --------- | ----- | ----------- | --------- | ---------- | ----- | ----- | --------- |
| FPN resnet50_v1 | 76.66 | 76.49 | 86.04 | 85.53   | 62.61  | 83.44 | 74.82 | 84.28 | 88.49 | 87.90 | 83.49  | 81.30 | 67.04       | 82.68     | 88.30 | 45.25       | 75.35     | 56.2178.80 | 78.80 | 60.97 | 84.22     |
## Pretrained model
The available pretrained model in D3 server is located in:
```bash
10.175.122.74:/development/l00506990/FaterRCNN_Benchmark
```
### Author
李旺灵(l00506990, liwangling@huawei.com)
2019.8.1

    ''')

    # 展示pandas数据框
    st.dataframe(pd.DataFrame([[1, 2], [3, 4]], columns=["a", "b"]))

    # 展示matplotlib绘图
    arr = np.random.normal(1, 1, size=100)
    plt.hist(arr, bins=20)
    plt.title("matplotlib plot")
    st.pyplot()

    # 加入交互控件，如输入框
    number = st.number_input("Insert a number", 123)
    st.write("输入的数字是：", number)


if __name__ == '__main__':
    main()
